package com.sgcc.ln.pmos.e_ln_home.activity;

import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.blankj.utilcode.util.StringUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.sgcc.pmos.nativesdk.R;
import com.sgcc.ln.pmos.arouter.home.HomeArouterPath;
import com.sgcc.ln.pmos.common.base.MvpBaseRecycleActivity;
import com.sgcc.ln.pmos.common.utils.DateUtils;
import com.sgcc.ln.pmos.e_ln_home.adapter.DCJZXXAdapter;
import com.sgcc.ln.pmos.e_ln_home.bean.DCJZXXBean;
import com.sgcc.ln.pmos.e_ln_home.bean.IformenCodeBean;
import com.sgcc.ln.pmos.e_ln_home.contract.DCJZXXContract;
import com.sgcc.ln.pmos.e_ln_home.presenter.DCJZXXPresenter;

import java.util.List;

/**
 * 电厂机组信息
 */
@Route(path = HomeArouterPath.HOME_DCJZXX)
public class DCJZXXActivity extends MvpBaseRecycleActivity<DCJZXXBean.DataBean.ListBean, DCJZXXPresenter> implements DCJZXXContract.View {

    private String applyId = "", name = "";
    private DCJZXXAdapter mDCJZXXAdapter;
    private TextView mTvFrom;
    private TextView mTvTime;
    private TextView mTvRange;
    private TextView mTvNumber;
    private String mOperateDate, mPublishType, mReadAmount;
    private boolean isFirst;
    private List<IformenCodeBean.DataBean.ChildrenBean> mList;

    @Override
    protected DCJZXXPresenter createPresenter() {
        return new DCJZXXPresenter();
    }

    @Override
    public void getDataSuccess(DCJZXXBean response) {
        if (response.getData() != null) {
            loadinglayout.showContent();
            if (response.getData().getList() != null) {
                int total = response.getData().getTotal();
                int pageNum = response.getData().getPageNum();
                int pageSize = response.getData().getPageSize();
                boolean nextPage = false;
                if (total != 0) {
                    // 优先计算总页数
                    int totalPage = total % pageSize == 0 ? total / pageSize : total / pageSize + 1;
                    //下一页等于当前页+1,但不能超过总页数界限
                    nextPage = pageNum + 1 <= totalPage ? true : false;
                }

                List<DCJZXXBean.DataBean.ListBean> list = response.getData().getList();
                for (DCJZXXBean.DataBean.ListBean bean : list) {
                    bean.setConnectVoltageOrder(queryStr(bean.getConnectVoltageOrder()));
                }
                loadSuccess(nextPage, list);
            } else {
                loadinglayout.showEmpty();
            }
        } else {
            loadinglayout.showError();
        }
    }

    private String queryStr(String encode) {
        String result = "";
        for (IformenCodeBean.DataBean.ChildrenBean bean : mList) {
            if (encode.equals(bean.getEncode())) {
                result = bean.getEncodeValue();
            }
        }
        return result;
    }

    @Override
    public String getTitleText() {
        return name + "电厂机组信息";
    }

    @Override
    public View getHeaderLayout(RelativeLayout parent) {
        View head_view = getLayoutInflater().inflate(R.layout.header_dcjzxx, parent, false);

        mTvFrom = (TextView) head_view.findViewById(R.id.tv_from);
        mTvTime = (TextView) head_view.findViewById(R.id.tv_time);
        mTvRange = (TextView) head_view.findViewById(R.id.tv_range);
        mTvNumber = (TextView) head_view.findViewById(R.id.tv_number);

        mTvFrom.setText(name);
        String time = "";
        try {
            time = StringUtils.isEmpty(DateUtils.getStrng_to_Times_ymdhms1(mOperateDate)) ?
                    DateUtils.get_TimesYMDHMSG1(mOperateDate) :
                    DateUtils.getStrng_to_Times_ymdhms1(mOperateDate);
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        mTvTime.setText(time);
        mTvRange.setText(mPublishType);
        mTvNumber.setText(mReadAmount);

        return head_view;
    }

    @Override
    public BaseQuickAdapter getAdapter() {
        mDCJZXXAdapter = new DCJZXXAdapter(mContext);
        return mDCJZXXAdapter;
    }

    @Override
    protected void receiveIntentExtras() {
        super.receiveIntentExtras();
        applyId = getIntent().getStringExtra("guid");
        name = getIntent().getStringExtra("sourceName");
        mOperateDate = getIntent().getStringExtra("operateDate");
        mPublishType = getIntent().getStringExtra("publishType");
        mReadAmount = getIntent().getStringExtra("readAmount");
        mPresenter.uniformencode();
    }

    @Override
    public void getDataRequest(int listSize, boolean isloading) {
        super.getDataRequest(listSize, isloading);
        if (isFirst) {
            mPresenter.getData(listSize, applyId, isloading);
        }
    }

    @Override
    public void getDataFailed() {

    }

    @Override
    public void uniformencodeSuccess(IformenCodeBean response) {
        if (response.getData().getChildren() != null) {
            if (response.getData().getChildren().size() > 0) {
                mList = response.getData().getChildren();
                isFirst = true;
                refresh(false);
            }
        }
    }

    @Override
    public void uniformencodeFailed() {

    }
}
